package com.ncu.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ncu.web.dao.mapper.ContactsMapper;
import com.ncu.web.dao.mapper.UserMapper;
import com.ncu.web.entry.Contacts;
import com.ncu.web.entry.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author:大润发杀鱼匠
 * @Date:2024/5/30 11:45 web
 */


@Component
public class ContactsService {

    @Autowired
    private ContactsMapper contactsMapper;

    public Contacts selectContactsById(int id) {
        return contactsMapper.selectContactsById(id);
    }


    public Contacts selectByEmail(String email){
        return contactsMapper.selectByEmail(email);
    }

    public Contacts selectByPhone(Long phone){
        return contactsMapper.selectByPhone(phone);
    }

    public ReturnData selectById(String id){
        return contactsMapper.selectById(id);
    }


    public List<ReturnData> selectByName(String name){
        return contactsMapper.selectByName(name);
    }

    public List<Contacts> selectByDim(int class_num, int entry_year, int graduate_year, String employers,
                                      String city, long phone, String email, int majority){

        QueryWrapper<Contacts> queryWrapper = new QueryWrapper<>();
        if (class_num != -1) {
            queryWrapper.eq("class_num", class_num);
        }
        if (entry_year != -1) {
            queryWrapper.eq("entry_year", entry_year);
        }
        if (graduate_year != -1) {
            queryWrapper.eq("graduate_year", graduate_year);
        }
        if (employers != null && !employers.isEmpty()) {
            queryWrapper.like("employers", employers);
        }
        if (city != null && !city.isEmpty()) {
            queryWrapper.like("city", city);
        }
        if (phone != -1) {
            queryWrapper.like("phone", phone);
        }
        if (email != null && !email.isEmpty()) {
            queryWrapper.like("email", email);
        }
        if (majority != -1) {
            queryWrapper.eq("majority", majority);
        }

        return contactsMapper.selectList(queryWrapper);

    }

    public int updateContacts(Contacts newContacts, Contacts oldContacts, Integer id, String name, String password) {
        UpdateWrapper<Contacts> updateWrapper = new UpdateWrapper<>();
        if (newContacts.getClassNum() != null) {
            updateWrapper.set("class_num", newContacts.getClassNum());
        }
        if (newContacts.getEntryYear() != null) {
            updateWrapper.set("entry_year", newContacts.getEntryYear());
        }
        if (newContacts.getGraduateYear() != null) {
            updateWrapper.set("graduate_year", newContacts.getGraduateYear());
        }
        if (!"".equals(newContacts.getEmployers())) {
            updateWrapper.set("employers", newContacts.getEmployers());
        }
        if (!"".equals(newContacts.getCity())) {
            updateWrapper.set("city", newContacts.getCity());
        }
        if (newContacts.getPhone() != null) {
            updateWrapper.set("phone", newContacts.getPhone());
        }
        if (!"".equals(newContacts.getEmail())) {
            updateWrapper.set("email", newContacts.getEmail());
        }
        if (newContacts.getMajority() != null) {
            updateWrapper.set("majority", newContacts.getMajority());
        }
        updateWrapper.eq("id", id);
        return contactsMapper.update(oldContacts, updateWrapper);
    }

    public List<ReturnData> selectAll(){
        return contactsMapper.selectAll();
    }



}
